//
//  FengShuiCore.m
//  PhongThuyHuongNha
//
//  Created by Le Anh Tai on 10/4/13.
//  Copyright (c) 2013 Le Anh Tai. All rights reserved.
//


#import "FengShuiCore.h"
//#import "UIConstants.h"


@implementation FengShuiCore

- (id)init
{
    self = [super init];
    
    if (self)
    {
        _quaiSo = [self calculateQuaiSo];
    }
    
    return self;
}

- (NSString*)genderString
{
    return _isMale ? @"Nam" : @"Nữ";
}

- (NSString *)getChi
{
    if (_lunarDate == nil)
    {
        return @"";
    }
    
    NSInteger year = [_lunarDate year];
    
    NSString *chi = [DIA_CHI objectAtIndex:(year - 4) % DIA_CHI.count];
    
    return chi;
}

- (NSString *)getCan
{
    if (_lunarDate == nil)
    {
        return @"";
    }
    
    NSInteger year = [_lunarDate year];
    NSString *can = [THIEN_CAN objectAtIndex:(year - 4) % THIEN_CAN.count];
    
    return can;
}

- (NSString *)getMang
{
    if (_lunarDate == nil)
    {
        return nil;
    }
    
    int year = [_lunarDate year];
    
    int index = (year - THUONG_NGUON_FROM)%60/2;
    return [MANGS objectAtIndex:index];
}

- (NSInteger)getQuaiSo
{
    return _quaiSo;
}

- (NSString *)getPhiCung
{
    NSInteger yearOld = [[NSDate date] year] - [_lunarDate year] + 1;//tính tuổi ta +1
    NSInteger quaiSo = _quaiSo;
    
    //    NSLog(@"Tinh phi cung cho %@ tuoi %d",_isMale? @"Nam":@"Nu",yearOld);
    
    if (_lunarDate == nil || yearOld <= 0)
    {
        return @"";
    }
    
    NSInteger index = 0;
    
    while (quaiSo < yearOld - 10)
    {
        quaiSo += 10;
        index++;
    }
    
    index += (yearOld-quaiSo);
    
    index = index % CUNG_PHI_NAM.count;
    
    if (_isMale)
    {
        return [CUNG_PHI_NAM objectAtIndex:index];
    }
    else
    {
        return [CUNG_PHI_NU objectAtIndex:index];
    }
}

- (NSString *)getPhiCungOfHuong:(NSString *)huong
{
    if ([huong isEqualToString:_HUONG_B]) return CUNG_PHI_KHAM;
    if ([huong isEqualToString:_HUONG_DB]) return CUNG_PHI_CAN1;
    if ([huong isEqualToString:_HUONG_D]) return CUNG_PHI_CHAN;
    if ([huong isEqualToString:_HUONG_DN]) return CUNG_PHI_TON;
    if ([huong isEqualToString:_HUONG_N]) return CUNG_PHI_LY;
    if ([huong isEqualToString:_HUONG_TN]) return CUNG_PHI_KHON;
    if ([huong isEqualToString:_HUONG_T]) return CUNG_PHI_DOAI;
    if ([huong isEqualToString:_HUONG_TB]) return CUNG_PHI_CAN2;
    
    return nil;
}

#pragma mark - Ngay gio Duong Lich Am Lich
- (NSString*)getGioAmLich
{
    if (_lunarDate == nil)
    {
        return nil;
    }
    
    NSInteger month = _lunarDate.month;
    NSInteger hour = _lunarDate.hour > 0 ? _lunarDate.hour : 24;
    NSInteger min = _lunarDate.minute;
    NSInteger deltaMin;
    NSString *gioAmLich;
    
    switch (month)
    {
        case 11:
            deltaMin = 10;
            break;
            
        case 10:
        case 12:
            deltaMin = 20;
            break;
            
        case 9:
        case 1:
            deltaMin = 30;
            break;
            
        case 8:
        case 2:
            deltaMin = 40;
            break;
            
        case 7:
        case 3:
            deltaMin = 50;
            break;
            
        case 6:
        case 4:
            deltaMin = 60;
            break;
            
        case 5:
            deltaMin = 70;
            break;
    }
    
    min += hour*60 - deltaMin;
    
    NSInteger index = min/2/60;
    
    gioAmLich = [DIA_CHI objectAtIndex:(index % 12)];
    
    return gioAmLich;
}

- (NSDate*)convertToLunarDate:(NSDate*)sunDate
{
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setDay:sunDate.day];
    [comps setMonth:sunDate.month];
    [comps setYear:sunDate.year];
    [comps setHour:sunDate.hour];
    [comps setMinute:sunDate.minute];
    
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *date = [gregorian dateFromComponents:comps];
    
    NSCalendar *budish = [[NSCalendar alloc] initWithCalendarIdentifier:NSChineseCalendar];
    NSUInteger unitFlags = NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit;
    NSDateComponents *components = [budish components:unitFlags fromDate:date];
    
    NSInteger day = [components day]; // 15
    NSInteger month = [components month]; // 9
    NSInteger year = [components year]; // 5764
    
//    Thuong Nguon: 1864 - 1923
//    Trung Nguon: 1924 - 1983
//    Ha Nguon: 1984 - 2043

    if ([self isThuongNguon:comps.year])
    {
        year += 1863;
    }
    else if ([self isTrungNguon:comps.year])
    {
        year += 1923;
    }
    else if ([self isHaNguon:comps.year])
    {
        year += 1983;
    }
    
    NSDate *lunarDate = [NSDate dateWithDay:day month:month year:year];
    
//    NSLog(@"sundate %@ - lunar date %@", [sunDate stringddmmyyyy], [lunarDate stringddmmyyyy]);
    
    return lunarDate;
}

- (BOOL) isThuongNguon:(NSInteger)year
{
    return year >= THUONG_NGUON_FROM && year <= THUONG_NGUON_TO;
}

- (BOOL) isTrungNguon:(NSInteger)year
{
    return year >= TRUNG_NGUON_FROM && year <= TRUNG_NGUON_TO;
}

- (BOOL) isHaNguon:(NSInteger)year
{
    return year >= HA_NGUON_FROM && year <= HA_NGUON_TO;
}

- (NSDate*)convertToSunDate:(NSDate*)lunarDate
{
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setDay:lunarDate.day];
    [comps setMonth:lunarDate.month];
    [comps setYear:lunarDate.year + SAI_SO_AM_LICH];
    
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSChineseCalendar];
    NSDate *date = [gregorian dateFromComponents:comps];
    
    NSCalendar *budish = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSUInteger unitFlags = NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit;
    NSDateComponents *components = [budish components:unitFlags fromDate:date];
    
    NSInteger day = [components day]; // 15
    NSInteger month = [components month]; // 9
    NSInteger year = [components year]; // 5764
    
    NSDate *sunDate = [NSDate dateWithDay:day month:month year:year];
    
    NSLog(@"lunarDate %@ - sunDate %@", [lunarDate stringddmmyyyy], [sunDate stringddmmyyyy]);
    
    return sunDate;
}

- (NSInteger)calculateQuaiSo
{
    NSInteger total = 0;
    NSString *year = [NSString stringWithFormat:@"%d", [[NSDate date] year]];
    
    for (int i = 0 ; i < year.length; i++)
    {
        unichar c = [year characterAtIndex:i];
        total += c - 48;
    }
    
    while (total > 9)
    {
        total -= 9;
    }
    
//    NSLog(@"Tinh quai so cho nam %d:%d", [[NSDate date] year], total);
    
    return total;
}

+ (NSInteger)numberOfDaySunInMonth:(int)month year:(int)year
{
    NSDateComponents *components = [[NSDateComponents alloc] init];
    
    [components setDay:1];
    [components setMonth:month];
    [components setYear:year];
    
    NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:components];
    
    NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSRange rng = [cal rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:date];
    NSUInteger numberOfDaysInMonth = rng.length;
    
    return numberOfDaysInMonth;
}

+ (NSInteger)numberOfDayLunarInMonth:(int)month year:(int)year
{
    NSDateComponents *components = [[NSDateComponents alloc] init];
    
    [components setDay:1];
    [components setMonth:month];
    [components setYear:year + SAI_SO_AM_LICH];
    
    NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:components];
    
    NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSChineseCalendar];
    
    NSRange rng = [cal rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:date];
    NSUInteger numberOfDaysInMonth = rng.length;
    
    return numberOfDaysInMonth;
}

#pragma mark - Thien Can - Dia Chi

- (NSString *)getHanhOfMonth:(NSInteger)month
{
    if (month > 12 || month < 1)
    {
        return @"";
    }
    
    switch (month)
    {
        case 1:
        case 2:
            return HANH_MOC;
            
        case 4:
        case 5:
            return HANH_HOA;
            
        case 7:
        case 8:
            return HANH_KIM;
            
        case 10:
        case 11:
            return HANH_THUY;
            
        default://3,6,9,12
            return HANH_THO;
    }
}

- (NSString *)getHanhOfThienCan:(NSString *)can
{
    if ([can isEqualToString:CAN_GIAP] || [can isEqualToString:CAN_AT])
    {
        return HANH_MOC;
    }
    
    if ([can isEqualToString:CAN_BINH] || [can isEqualToString:CAN_DINH])
    {
        return HANH_HOA;
    }
    
    if ([can isEqualToString:CAN_MAU] || [can isEqualToString:CAN_KY])
    {
        return HANH_THO;
    }
    
    if ([can isEqualToString:CAN_CANH] || [can isEqualToString:CAN_TAN])
    {
        return HANH_KIM;
    }
    
    if ([can isEqualToString:CAN_NHAM] || [can isEqualToString:CAN_QUY])
    {
        return HANH_THUY;
    }
    
    return @"";
}

- (NSString *)getHanhOfDiaChi:(NSString *)chi
{
//    - Hợi Tý thuộc Thủy.
//    - Dần Mẹo thuộc Mộc.
//    - Tỵ Ngọ thuộc Hỏa.
//    - Thân Dậu thuộc Kim
//    - Thìn Tuất Sửu Mùi thuộc Thổ.
    
    if ([chi isEqual:CHI_HOI] || [chi isEqual:CHI_TY1])
    {
        return HANH_THUY;
    }
    
    if ([chi isEqual:CHI_DAN] || [chi isEqual:CHI_MEO])
    {
        return HANH_MOC;
    }
    
    if ([chi isEqual:CHI_TY5] || [chi isEqual:CHI_NGO])
    {
        return HANH_HOA;
    }
    
    if ([chi isEqual:CHI_THAN] || [chi isEqual:CHI_DAU])
    {
        return HANH_KIM;
    }
    
    if ([chi isEqual:CHI_THIN] || [chi isEqual:CHI_TUAT] ||
        [chi isEqual:CHI_SUU] || [chi isEqual:CHI_MUI])
    {
        return HANH_THO;
    }
    
    return @"";
}

//- Hợi Mẹo Mùi. - Tỵ Dậu Sửu. - Thân Tý Thìn. - Dần Ngọ Tuất.
- (NSArray*)getTamHapOfChi:(NSString*)chi
{
    NSInteger index = [DIA_CHI indexOfObject:chi];
    
    if (index < 0 || index >= DIA_CHI.count)
    {
        return nil;
    }
    
    NSString *chi1 = [DIA_CHI objectAtIndex:index];
    NSString *chi2 = [DIA_CHI objectAtIndex:(index + 4) % DIA_CHI.count];
    NSString *chi3 = [DIA_CHI objectAtIndex:(index + 8) % DIA_CHI.count];
    
    return [NSArray arrayWithObjects:chi1, chi2, chi3, nil];
}

//Sửu - Dần - Mẹo  - Thìn - Tỵ   - Ngọ
//Tý  - Hợi - Tuất - Dậu  - Thân - Mùi
- (NSString*)getLucHapOfChi:(NSString*)chi
{
    NSArray *array1 = [NSArray arrayWithObjects:CHI_SUU, CHI_DAN, CHI_MEO, CHI_THIN, CHI_TY5, CHI_NGO, nil];
    NSArray *array2 = [NSArray arrayWithObjects:CHI_TY1, CHI_HOI, CHI_TUAT, CHI_DAU, CHI_THAN, CHI_MUI, nil];
    
    NSInteger index = [array1 indexOfObject:chi];
    
    if (index > -1 && index < array1.count)
    {
        return [array2 objectAtIndex:index];
    }
    else
    {
        index = [array2 indexOfObject:chi];
        
        if (index > -1 && index < array2.count)
        {
            return [array1 objectAtIndex:index];
        }
    }
    
    return @"";
}

//Xung: - Tý Ngọ Mẹo Dậu. - Thìn Tuất Sửu Mùi. - Dần Thân Tỵ Hợi.
- (NSArray*)getTuHanhXungOfChi:(NSString*)chi
{
    NSInteger index = [DIA_CHI indexOfObject:chi];
    
    if (index < 0 || index >= DIA_CHI.count)
    {
        return nil;
    }
    
    NSString *chi1 = [DIA_CHI objectAtIndex:index];
    NSString *chi2 = [DIA_CHI objectAtIndex:(index+6) % DIA_CHI.count];
    NSString *chi3 = [DIA_CHI objectAtIndex:(index+3) % DIA_CHI.count];
    NSString *chi4 = [DIA_CHI objectAtIndex:(index+9) % DIA_CHI.count];
    
    return [NSArray arrayWithObjects:chi1, chi2, chi3, chi4, nil];
}

//Xung: Tý Ngọ. - Sửu Mùi. - Dần Thân. - Mẹo Dậu. - Thìn Tuất. - Tỵ Hợi. Là lục xung.
- (NSString*)getLucXungOfChi:(NSString*)chi
{
    NSInteger index = [DIA_CHI indexOfObject:chi];
    
    if (index < 0 || index >= DIA_CHI.count)
    {
        return @"";
    }
    
    return [DIA_CHI objectAtIndex:(index+6)%DIA_CHI.count];
}

//Hại: Tý Mùi. - Sửu Ngọ. - Dần Tỵ. - Mẹo Thìn. - Hợi Thân. - Tuất Dậu. Là lục hại
- (NSString*)getLucHaiOfChi:(NSString*)chi
{
    NSInteger index = [DIA_CHI indexOfObject:chi];
    
    if (index < 0 || index >= DIA_CHI.count)
    {
        return @"";
    }
    
    NSInteger rangeTy1Mui = 7;
    NSInteger rangeThanHoi = 19;
    
    if (index <= rangeTy1Mui)
    {
        return [DIA_CHI objectAtIndex:rangeTy1Mui - index];
    }
    else
    {
        return [DIA_CHI objectAtIndex:rangeThanHoi - index];
    }
}
@end
